﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Security;

namespace BtoBSWT315.User
{
    public partial class Modify_hy : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!IsPostBack)
            {
                EditDataBind();
            }
        }
        private void EditDataBind()
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }

            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            //定义所需SQL语句
            string sql = "select * from qyml where [user]='" + Session["user"] + "'";
            OleDbDataReader dr = classdb.Getdr(sql);
            if (dr.Read())
            {
                user.Text = dr["user"].ToString();             //用户名
                address.Text = dr["address"].ToString();       //街道地址
                phone.Text = dr["phone"].ToString();           //电话
                name.Text = dr["name"].ToString();             //联系人
                fax.Text = dr["fax"].ToString();               //传真
                email.Text = dr["email"].ToString();           //电子邮件
                web.Text = dr["web"].ToString();               //网址
                mobile.Text = dr["mobile"].ToString();         //手机
                city.Text = dr["city"].ToString();       //城市
                sf.Text = dr["sf"].ToString();            //省份
            }
            dr.Close();
        }



        private void Button1_Click(object sender, System.EventArgs e)
        {
            if (pass.Text != confirmPassword.Text)
            {
                Response.Write("<script>alert('确认密码不对，请重新输入');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }
            //定义所需SQL
            if ("" == pass.Text)   //密码没修改
            {
                //string sql="update qyml set address='"+address.Text+"'";
                //sql+=",phone='"+phone.Text+"',name='"+name.Text+"',fax='"+fax.Text+"'";
                //sql+=",email='"+email.Text+"',web='"+web.Text+"',mobile='"+mobile.Text+"'";
                //sql+=",sf='"+sf.SelectedValue+"',city='"+city.SelectedValue+"'";
                //sql+=" where [user]='"+Session["user"]+"'";

                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

                string sql = "update qyml set [address]=@address,[phone]=@phone,[name]=@name,[fax]=@fax,[email]=@email,[web]=@web,[mobile]=@mobile,[sf]=@sf,[city]=@city where [user]=@user";
                List<OleDbParameter> parameters = new List<OleDbParameter>();
                parameters.Add(new OleDbParameter("@address", address.Text.Trim()));
                parameters.Add(new OleDbParameter("@phone", phone.Text.Trim()));
                parameters.Add(new OleDbParameter("@name", name.Text.Trim()));
                parameters.Add(new OleDbParameter("@fax", fax.Text.Trim()));
                parameters.Add(new OleDbParameter("@email", email.Text.Trim()));
                parameters.Add(new OleDbParameter("@web", web.Text.Trim()));
                parameters.Add(new OleDbParameter("@mobile", mobile.Text.Trim()));
                parameters.Add(new OleDbParameter("@sf", sf.Text.Trim()));
                parameters.Add(new OleDbParameter("@city", city.Text.Trim()));
                parameters.Add(new OleDbParameter("@user", Session["user"].ToString()));

                //引入数据库操作类
                if (classdb.GetNonResult(sql, parameters.ToArray()) != 0)
                {
                    Response.Write("<script>alert('操作成功');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('操作失败！请重新操作！');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }

            if ("" != pass.Text)   //密码已经修改
            {
                //取得用户密码并加密
                string AdminPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Text, "MD5").ToLower().Substring(8, 16);

                string sql = "update qyml set pass='" + AdminPassword + "',address='" + address.Text.Trim() + "'";
                sql += ",phone='" + phone.Text.Trim() + "',name='" + name.Text.Trim() + "',fax='" + fax.Text.Trim() + "'";
                sql += ",email='" + email.Text.Trim() + "',web='" + web.Text.Trim() + "',mobile='" + mobile.Text.Trim() + "'";
                sql += ",sf='" + sf.Text.Trim() + "',city='" + city.Text.Trim() + "'";
                sql += " where [user]='" + Session["user"] + "'";
                //引入数据库操作类
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                if (classdb.GetNonResult(sql) != 0)
                {
                    Response.Write("<script>alert('操作成功');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('操作失败！请重新操作！');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }
        }


        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}